package com.hygj.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hygj.pojo.PrintOffice;
import com.hygj.service.PrintOfficeService;
import com.hygj.util.PageUtil;

@Controller
@RequestMapping
public class PrintOfficeController {
   
	@Autowired
	private PrintOfficeService printOfficeService;
	
	/**
	 * 保存
	 * @param map
	 * @param printOffice
	 * @return
	 */
	@RequestMapping("insertPrintOffice")
	public String insertPrintOffice(Map<String, Object> map, PrintOffice printOffice) {
		printOfficeService.insert("insert", printOffice);
		map.put("printOffice", printOffice);
		return "redirect:getPrintOfficeAll";
	}
	
	/**
	 * 查询全部
	 * 
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping("getPrintOfficeAll")
	public String getPrintOfficeAll(Map<String, Object> map,HttpServletRequest request) {
		Map<String, Object> params = new HashMap<String, Object>();		
		PageUtil<PrintOffice> page = printOfficeService.page(request, 10, params);
		map.put("page", page);
		return "prints/printOfficeShow";
	}
	
	/**
	 * 修改(印刷服务单位名称)(禁用|启用)
	 * @param printOffice
	 * @return
	 */
	@RequestMapping("updatePrintOffice")
	public String updatePrintOffice(PrintOffice printOffice){
		printOfficeService.update("update", printOffice);
		return "redirect:getPrintOfficeAll";
	}
	
	/**
	 * 详情页面(单位修改用)
	 * @param id
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("getPrintOfficeById")
	public String getPrintOfficeById(@RequestParam("id") Integer id, ModelMap modelMap) {
		PrintOffice printOffices = printOfficeService.getById("getById", id);
		modelMap.addAttribute("printOffices", printOffices);
		return "prints/printOfficeUpdate";
	}
	
	/**
	 * 详情页面(禁用|启用)
	 * @param id
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("getPrintOfficeByIds")
	public String getPrintOfficeByIds(@RequestParam("id") Integer id, ModelMap modelMap) {
		PrintOffice printOffices = printOfficeService.getById("getById", id);
		modelMap.addAttribute("printOffices", printOffices);
		return "prints/printOfficeOpen";
	}
	
	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("deletePrintOffice")
	public String deletePrintOffice(@RequestParam("id") Integer id) {
		printOfficeService.delete("delete", id);
		return "redirect:getPrintOfficeAll";
	}
	
	
	/**
	 * 随机抽取
	 * 
	 * @param response
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("print")
	public List<PrintOffice> randomNum(HttpServletResponse response, HttpServletRequest request) {
		List<PrintOffice> printOffices = printOfficeService.findRandomByName("启用");
		return printOffices;
	}
	
}
